package ml.uuku.oschub.adpater;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import ml.uuku.oschub.AppContext;
import ml.uuku.oschub.R;
import ml.uuku.oschub.bean.Tweet;
import ml.uuku.oschub.emoji.InputHelper;
import ml.uuku.oschub.util.PlatfromUtil;
import ml.uuku.oschub.util.StringUtils;
import ml.uuku.oschub.util.TypefaceUtils;
import ml.uuku.oschub.widget.MyLinkMovementMethod;
import ml.uuku.oschub.widget.MyURLSpan;
import ml.uuku.oschub.widget.TweetTextView;

/**
 * @author HuangWenwei
 * @author kymjs
 * @date 2014年10月10日
 */
public class TweetAdapter extends ListBaseAdapter<Tweet> {

    static class ViewHolder {
        @Bind(R.id.tv_tweet_name)
        TextView author;
        @Bind(R.id.tv_tweet_time)
        TextView time;
        @Bind(R.id.tweet_item)
        TweetTextView content;
        @Bind(R.id.tv_tweet_comment_count)
        TextView commentcount;
        @Bind(R.id.tv_tweet_platform)
        TextView platform;
        @Bind(R.id.iv_tweet_face)
        ImageView face;
        @Bind(R.id.iv_tweet_image)
        ImageView image;
        @Bind(R.id.tv_like_state)
        TextView tvLikeState;
        @Bind(R.id.tv_del)
        TextView del;
        @Bind(R.id.tv_likeusers)
        TextView likeUsers;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private Context context;

    @Override
    protected View getRealView(final int position, View convertView,
            final ViewGroup parent) {
        context = parent.getContext();
        final ViewHolder vh;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_tweet, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final Tweet tweet = mDatas.get(position);

        vh.del.setVisibility(View.GONE);
        vh.author.setText(tweet.getAuthor());
        vh.time.setText(StringUtils.friendly_time(tweet.getPubDate()));
        vh.content.setMovementMethod(MyLinkMovementMethod.a());
        vh.content.setFocusable(false);
        vh.content.setDispatchToParent(true);
        vh.content.setLongClickable(false);

        Spanned span = Html.fromHtml(tweet.getBody().trim());

        if (!StringUtils.isEmpty(tweet.getAttach())) {

            SpannableString str = new SpannableString("c");
            vh.content.setText(str);
            span = InputHelper.displayEmoji(context.getResources(), span);
            vh.content.append(span);
        } else {
            span = InputHelper.displayEmoji(context.getResources(), span);
            vh.content.setText(span);
        }
        MyURLSpan.parseLinkText(vh.content, span);

        vh.commentcount.setText(tweet.getCommentCount() + "");
        tweet.setLikeUsers(context, vh.likeUsers, true);
        final ViewHolder vh1 = vh;
        if (tweet.getLikeUser() == null) {
            vh.tvLikeState.setVisibility(View.GONE);
        }
        TypefaceUtils.setTypeface(vh.tvLikeState);
        if (tweet.getIsLike() == 1) {
            vh.tvLikeState.setTextColor(AppContext.getInstance().getResources().getColor(R.color.day_colorPrimary));
        } else {
            vh.tvLikeState.setTextColor(AppContext.getInstance().getResources().getColor(R.color.gray));
        }
        PlatfromUtil.setPlatFromString(vh.platform, tweet.getAppclient());

        return convertView;
    }

}
